package com.tepth.remote.control.delegate.launcher;

import com.tepth.remote.control.base.bean.Config;
import com.tepth.remote.control.base.convert.IStringDataConvert;
import com.tepth.remote.control.manager.ConfigManager;
import com.tepth.remote.core.net.callback.IError;

import org.jetbrains.annotations.NotNull;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/8
 */
public class LauncherPresenter extends LauncherContract.AbstractPresenter {

    private final LauncherContract.View mView;
    private final LauncherContract.Model mModel;

    public LauncherPresenter(@NotNull LauncherContract.View view) {
        mView = view;
        mModel = new LauncherModel();
    }

    @Override
    void getConfig(String imei) {
        mView.showLoading("正在登录");
        addDisposable(mModel.provideConfigData(imei, new IStringDataConvert<Config>() {
            @Override
            public void convert(Config config) {
                mView.hideLoading();
                if (config != null) {
                    ConfigManager.getInstance()
                            .setRealName(config.getRealName())
                            .setUid(config.getUid())
                            .setPhoneId(config.getPhoneId())
                            .setIsBm(config.getIsBm())
                            .setIsZj(config.getIsZj())
                            .setProjects(config.getProjects());
                    mView.loginSuccess();
                } else {
                    mView.loginFailed(500, "系统未找到该手机串号");
                }
            }
        }, new IError() {
            @Override
            public void onError(int code, String msg) {
                mView.loginFailed(code, msg);
            }
        }));
    }
}
